package com.example.zheng.controller;


import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zheng.entity.Admin;
import com.example.zheng.entity.Repairman;
import com.example.zheng.entity.Student;
import com.example.zheng.service.IAdminService;
import com.example.zheng.service.IRepairmanService;
import com.example.zheng.service.IStudentService;
import com.example.zheng.token.JwtUtil;
import com.example.zheng.util.AcceptUtil;
import com.example.zheng.util.FastDFSUtil;
import com.example.zheng.util.Result;
import org.csource.common.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @date 2022/1/11 - 10:32
 */
@RestController
public class LoginController {

    @Autowired
    private IStudentService iStudentService;

    @Autowired
    private IAdminService iAdminService;

    @Autowired
    private IRepairmanService iRepairmanService;

    @PostMapping("/login")
    public Result login(@RequestBody AcceptUtil acceptUtil, HttpSession session){

        String username = acceptUtil.getUsername();
        String password = acceptUtil.getPassword();
        String identity = acceptUtil.getIdentity();

        //token存入信息
        Map<String,String> Tokenmap = new HashMap<>();

        //什么身份查什么表
        if (identity.equals("学生")){
            QueryWrapper<Student> wrapper = new QueryWrapper<>();
            wrapper.eq("student_number",username).eq("student_password",password);
            Student student = iStudentService.getOne(wrapper);
            if (student==null){
                return Result.failure("账号或密码错误");
            }
            //用与获取个人信息
            Tokenmap.put("name",student.getStudentName());
            Tokenmap.put("phone",student.getStudentTel());
            Tokenmap.put("address",student.getStudentAddress());

        }else if(identity.equals("管理员")){
            QueryWrapper<Admin> wrapper = new QueryWrapper<>();
            wrapper.eq("admin_id",username).eq("admin_password",password);
            Admin admin = iAdminService.getOne(wrapper);
            if (admin==null){
                return Result.failure("账号或密码错误");
            }
            Tokenmap.put("name",admin.getAdminName());
            Tokenmap.put("phone",admin.getAdminTel());
            Tokenmap.put("address",admin.getAdminAddress());
        }else if(identity.equals("维修员")){
            QueryWrapper<Repairman> wrapper = new QueryWrapper<>();
            wrapper.eq("repairman_id",username).eq("repairman_password",password);
            Repairman repairman = iRepairmanService.getOne(wrapper);
            if (repairman==null){
                return Result.failure("账号或密码错误");
            }
            Tokenmap.put("name",repairman.getRepairmanName());
            Tokenmap.put("phone",repairman.getRepairmanTel());
            Tokenmap.put("address",repairman.getRepairmanAddress());

            System.out.println("repairman============"+repairman);
            session.setAttribute("repairman",repairman);

        }else {
            return Result.failure("请选择你的身份");
        }

        //token封装登录信息
        Tokenmap.put("username",username);
        Tokenmap.put("password",password);
        Tokenmap.put("identity",identity);
        String token = JwtUtil.getToken(Tokenmap);

        //封装返回信息
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("token",token);

        acceptUtil.setPassword(null);
        resultMap.put("user",acceptUtil);
        return Result.success(resultMap);
    }


    //文件上传接口
    @PostMapping("/uploadPicture")
    private String test(MultipartFile file) throws MyException, IOException {
        String s = FastDFSUtil.uploadFile(file);
        return s;
    }

    @GetMapping("/getUserDetail")
    private Result getUserDetail(@RequestHeader String authorization){

        DecodedJWT decodedJWT = JwtUtil.getDecodedJWT(authorization);
       // Claim phone = decodedJWT.getClaim("phone");
        Map<String, Claim> claims = decodedJWT.getClaims();
        System.out.println(claims);

        HashMap<String, String> map = new HashMap<>();
        map.put("id", claims.get("username").asString());
        map.put("name", claims.get("name").asString());
        map.put("identity", claims.get("identity").asString());
        map.put("phone", claims.get("phone").asString());
        map.put("address",claims.get("address").asString());

        return Result.success("获取成功",map);
    }
}
